package com.starbaba.activity.view;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.activity.service.ActivityLoanOrderRecordService;
import com.starbaba.activity.service.impl.ActivityLoanOrderRecordServiceImpl;
import com.starbaba.activityCommend.model.LoanActivityModel;
import com.starbaba.activityCommend.service.LoanProductInfoService;
import com.starbaba.activityCommend.service.impl.LoanProductInfoServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.tuanzi.server.loan_base.model.db.activity.ActivityLoanOrderRecord;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 借够钱活动用户登记action
 * @author chenweijun
 *
 */
@SuppressWarnings({"serial","unchecked","rawtypes","unused"})
public class ActivityLoanOrderRecordAction extends BaseAction implements ModelDriven<ActivityLoanOrderRecord> {
	
	private ActivityLoanOrderRecord model;
	private ActivityLoanOrderRecordService service; 
	private LoanProductInfoService infoService;
	
	private InputStream inputStream;   //导出文件输入流
	private String fileName;   //导出文件名
	
	public ActivityLoanOrderRecordAction(){
		this.service = new ActivityLoanOrderRecordServiceImpl();
		this.infoService = new LoanProductInfoServiceImpl();
	}

	@Override
	public ActivityLoanOrderRecord getModel() {
		if(model == null){
			model = new ActivityLoanOrderRecord();
		}
		return model;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public String show(){
		parsePageRequest();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		List<LoanActivityModel> activityList = infoService.getList();
		if(activityList != null && activityList.size() > 0){
			paramsMap.put("activityList", activityList);
			return SUCCESS;
		}
		return ERROR;
	}
	
	public void getList(){
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			List<ActivityLoanOrderRecord> list =  service.getList(paramsMap, pageRequest.getIDisplayStart(),pageRequest.getIDisplayLength());
			//对于mobnum中间4位转为*
			for (int i = 0; i <list.size() ; i++) {
				ActivityLoanOrderRecord activityLoanOrderRecord = list.get(i);
				activityLoanOrderRecord.setMobnum(PhoneNumNideUtil.hideNum(activityLoanOrderRecord.getMobnum()));
				list.set(i,activityLoanOrderRecord);
			}
			int count=service.countList(paramsMap);
			pageResponse.setITotalDisplayRecords(count);
			pageResponse.setITotalRecords(count);
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch(IOException e) {
			LogService.error(e);
		}
		
	}
	
	public String exportList(){
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
	        try {
	        	  String result = service.exportList(paramsMap);
				  inputStream = new ByteArrayInputStream(result.getBytes("GBK"));
				  fileName = "activity_loan_order_export_" + System.currentTimeMillis() + ".csv";
			} catch (UnsupportedEncodingException e) {
				LogService.error(e);
				return ERROR;
			}
			return SUCCESS;
		
	}


}
